package com.example.recyclerviewstudy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.example.recyclerviewstudy.Adapter.firstRecyclerAdapter.DividerGridItemDecoration;
import com.example.recyclerviewstudy.Adapter.firstRecyclerAdapter.DividerItemDecoration;
import com.example.recyclerviewstudy.Adapter.firstRecyclerAdapter.FirstRecyclerViewAdapter;
import com.example.recyclerviewstudy.Adapter.firstRecyclerAdapter.OnItemClickListener;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private FirstRecyclerViewAdapter mFirstRecyclerViewAdapter;
    private SparseArray<String> ItemDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();


    }

    private void initData() {
        if (ItemDatas == null)
            ItemDatas = new SparseArray<>();
        for (int i = 'A'; i < 'z'; i++) {
            ItemDatas.append(i, "" + (char) i);
        }
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recycle_view);
        //set LayoutManager
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setAutoMeasureEnabled(true);//是否自动测绘  默认是false  但是兼容包中提供的3种LayoutManager默认为true

        //mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
        //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL));
        //set ItemAnimator
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //add itemDecoration
        //mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST));
        if (mFirstRecyclerViewAdapter == null)
            mFirstRecyclerViewAdapter = new FirstRecyclerViewAdapter(ItemDatas);
        mRecyclerView.setAdapter(mFirstRecyclerViewAdapter);

        mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
                return false;
            }

            @Override
            public void onTouchEvent(RecyclerView rv, MotionEvent e) {

            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
        mFirstRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + "click", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position) {
                Toast.makeText(MainActivity.this, position + " long click", Toast.LENGTH_SHORT).show();
                mFirstRecyclerViewAdapter.removeItem(position);
            }
        });
    }


}
